package com.cea.papyrus.core.helper;

import java.util.ArrayList;
import java.util.Iterator;

import org.eclipse.draw2d.geometry.Dimension;
import org.eclipse.draw2d.geometry.Point;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.PlatformUI;
import org.eclipse.uml2.uml.Element;
import org.eclipse.uml2.uml.Profile;

import com.cea.papyrus.core.ui.dialogs.PaletteChooseSetMetaclassDialog;
import com.cea.papyrus.core.utils.Debug;
import com.cea.papyrus.diagraminterchange2.di2.Diagram;
import com.cea.papyrus.diagraminterchange2.di2.GraphElement;
import com.cea.papyrus.diagraminterchange2.di2.GraphNode;
import com.cea.papyrus.diagraminterchange2.di2.Uml1SemanticModelBridge;
import com.cea.papyrus.profile.ImageManager;

public class NodeMetaclassHelper extends NodeClassHelper {

	public GraphNode createGraphElement(Element umlChild, GraphElement parent, Point location, Dimension size){
		Debug.debug(this,"Call NodeClassFactory.createGraphNode()", channel); //$NON-NLS-1$
		
		
		// creates compartments for class figure (operation and attribute)
		NodeHelper nh = new NodeHelper();
		GraphNode graphNode=nh.createGraphElement(umlChild, parent, location, size);
		
		return graphNode;
	}
	
	

	public ArrayList createGraphNode(Element umlElement, Diagram diagram) {

		Debug.debug(this,"Call NodeMetaclassFactory.createGraphNode()", channel);
		ArrayList createdNode =super.createGraphNode(umlElement, diagram);

		Iterator it =createdNode.iterator();
		while(it.hasNext()){
			//current node
			GraphNode currentNode=(GraphNode)it.next();
			currentNode.setSize(new Dimension(100,50));

			//currentNode.setPosition(new Point(20, 20));
		}
		return createdNode;
	}
	
	
	public boolean canCreateGraphElement(GraphElement parent){
		if(parent instanceof Diagram) {
			Element umlpackage = ((Uml1SemanticModelBridge)((Diagram)parent).getOwner()).getElement();
			return canCreateUMLElement(umlpackage);
			
		} else if (parent.getSemanticModel()!=null){
			Element parentElt= ((Uml1SemanticModelBridge)parent.getSemanticModel()).getElement();
			if(parentElt!=null){
				return canCreateUMLElement(parentElt);
			}
		}
		return false;
	}

	public  Element createUMLElement(String name, org.eclipse.uml2.uml.Element umlParent){
		return (Element)this.createCollectionUMLElement(umlParent).get(0);
	}

	/**
	 * 
	 * @param umlParent
	 * @return All elements we want to display
	 */
	public  ArrayList createCollectionUMLElement(org.eclipse.uml2.uml.Element umlParent){
		// futur selected metaclasses
		ArrayList<org.eclipse.uml2.uml.ElementImport> selectedMetaclasses = new ArrayList<org.eclipse.uml2.uml.ElementImport>();
		
		// import metaclass(es) 
		
		if (umlParent instanceof Profile){
			
			// opening of a message box indicating profile application
			final Shell message = new Shell(new Shell(), SWT.APPLICATION_MODAL | SWT.SHELL_TRIM);
			message.setText("Metaclass Import");
			message.setImage(ImageManager.IMG_METACLASS);
			message.setSize(250, 100);
			
		    final Label label = new Label(message, SWT.NONE);
		    label.setText("Fetching metaclasses...");
		    label.setBounds(20, 15, 150, 20);
			
			message.open();

			
			PaletteChooseSetMetaclassDialog cmcd= new PaletteChooseSetMetaclassDialog(
					PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(),
					((Profile)umlParent),
					false);
			cmcd.open();
			
			for(int i=0; i<cmcd.getSelectedElements().size(); i++){
				selectedMetaclasses.add((org.eclipse.uml2.uml.ElementImport)cmcd.getSelectedElementImports().get(i));
			}
			
			message.close();
		}
		
		return selectedMetaclasses;

	}
	
	public boolean canCreateUMLElement(Element umlParent) {
		if(umlParent instanceof Profile) {
			return true;
		}
		return false;
	}
	
}
